package com.qixin.api.serviceimpl;

import com.alibaba.fastjson.JSONObject;
import com.qixin.api.bean.*;
import com.qixin.api.bean.pojo.ChargingInfo;
import com.qixin.api.mapper.ComStationinfoMapper;
import com.qixin.api.service.ComConnectorinfoService;
import com.qixin.api.service.ComEquipmentinfoService;
import com.qixin.api.service.ComStationinfoPictureService;
import com.qixin.api.service.ComStationinfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.qixin.util.chargeInterfce.QiXinPublic.StarInterFce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 充电站信息
用于描述充电站的信息，包含充电站的基本信息、服务信息、支付信息等 服务实现类
 * </p>
 *
 * @author 张子艺
 * @since 2018-10-21
 */
@Service
public class ComStationinfoServiceImpl extends ServiceImpl<ComStationinfoMapper, ComStationinfo> implements ComStationinfoService {
   @Autowired
    ComEquipmentinfoService comEquService;
    @Autowired
    ComConnectorinfoService comConService;

    @Autowired
    ComStationinfoPictureService comPictureService;
    @Override
    public  StationInfo  findStationInfo(int PageNo,int PageSize) {
        StarInterFce starInterFce=new StarInterFce();
        String token=starInterFce.getToken( );
        String stationInfo=starInterFce.getChargeQuery(token,null,PageNo,PageSize);
        StationInfo comStationinfo=  JSONObject.parseObject(stationInfo,StationInfo.class);
        return  comStationinfo;
    }

    @Override
    public List<ComStationinfo> findNearbyStation(String lat, String lng,String param) {
        return baseMapper.findNearbyStation(lat,lng,param);
    }

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public boolean dateInsert() throws SQLException {
        this.deletAll();
        comEquService.deletAll();
        comConService.deletAll();
        comPictureService.deletAll();


        List<StationInfo> stationinfoList=new ArrayList<>();
        //从接口中拿到数据  本操作为了一个请求拿到接口中所有数据
        //随意定义循环次数
        int loop=5;
        for (int i = 1; i < loop; i++) {
            StationInfo stationInfo=findStationInfo(i,200);
            stationinfoList.add(stationInfo);
            //第一次循环的时候 就修改循环次数
            if(i==1) {
                loop = stationInfo.getPageCount() + 1;
            }
        }

          //接口中所有充电桩信息都在集合中 每一页就是一个元素   循环所有页元素
        for (int o = 0; o <stationinfoList.size() ; o++) {
            StationInfo stationInfo=  stationinfoList.get(o);
        //把充电站信息拿到
        List<ComStationinfo>  comStationInfos=stationInfo.getStationinfos();
        //循环充电站 准备添加数据库
        for (int i = 0; i < comStationInfos.size(); i++) {
            //拿到每一个充电站
            ComStationinfo comStationInfo=comStationInfos.get(i);
            //保存
            if(!this.insert(comStationInfo)){
                throw new SQLException();
            };

              String [] pictures=comStationInfo.getPictures();
            //给每一个充电站的图片 关联
            for (int l = 0; l < pictures.length; l++) {
                ComStationinfoPicture comPicture=new ComStationinfoPicture();
                comPicture.setRelactionId(comStationInfo.getId());
                comPicture.setPicture(pictures[l]);
                comPicture.setStationID(comStationInfo.getStationID());
                if(!comPictureService.insert(comPicture)){
                    throw new SQLException();
                };
            }


           //拿到每个充电站中的充电桩
           List<ComEquipmentinfo>  comEqus=comStationInfos.get(i).getEquipmentInfos();
            for (int j = 0; j < comEqus.size(); j++) {
                //拿到每一个充电桩
                ComEquipmentinfo comEqu=comEqus.get(j);
                //给每一个充电桩绑定充电站ID
                comEqu.setRelactionId(comStationInfo.getId());
                if(!comEquService.insert(comEqu)) {
                    throw new SQLException();
                };

                List<ComConnectorinfo> comCons=comEqu.getConnectorInfos();
                for (int k = 0; k < comCons.size(); k++) {
                    //拿到每一个充电桩的枪口
                    ComConnectorinfo  comCon=comCons.get(k);
                    //给每个枪口绑定上充电桩ID
                      comCon.setRelactionId(comEqu.getId());
                    if(!comConService.insert(comCon)){
                        throw new SQLException();
                    };


                }
            }
        }
        }
        return true;
    }

    @Override
    public int deletAll() {
        return baseMapper.deletAll();
    }

    @Override
    public List<ChargingInfo> findChargeInfo(Long id) {
        return baseMapper.findChargeInfo(id);
    }
}
